package sip;

//import com.sun.org.apache.xml.internal.security.Init;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DecimalFormat;

/**
 * Class to handle the configuration file
 * file contains amount of battery, amount in reservoir, and active profile
 * @author Stephen
 */
public class InitSip {
    //private static double mBatteryLevel;
    //private static double mReservoirAmount;
    public static int batteryLife = 259200; //(3 days of dration ) the amount of seconds that a new Battery has available;
    public static int secondsUsed; //amount of seconds used by the battery. They are counted in the Battery Label
    public static String mActiveProfileName;
    public static double totalInsulinAmount;
    public static double BatteryLevel;
    static private String msConfigFileName = "sipConfig.txt";
    private static String[] configVals = {"0", "0", "0"};
    public static Boolean bubbles = false;
   
    ///End of Line characters are OS dependent
    private static String eol = System.getProperty("line.separator");
    private double correctedBatteryLevel;
    
    
    /** constructor */
    public InitSip() {}
    
    public synchronized void initialize()
    {
        //read in values from config file
        SipLogger.Instance().info("Initializing Simple Insulin Pump");
        File f = new File(msConfigFileName);
        if(f.exists())
        {   //reads in buffer form, instead of line by line
            try {
                BufferedReader configFile = new BufferedReader(new FileReader(msConfigFileName));
                for(int x=0; x<3; x++ ){
                    configVals[x] = configFile.readLine();
                }
                BatteryLevel = new Double(String.valueOf(configVals[0]));
                totalInsulinAmount = new Double(String.valueOf(configVals[1]));
                mActiveProfileName = configVals[2];
                SipLogger.Instance().debug("Initializing - Current Battery Level: " + configVals[0]);
                SipLogger.Instance().debug("Initializing - Current Insulin Amount: " + configVals[1]);
                SipLogger.Instance().debug("Initializing - Active Profile name, when SIP was running last: " + configVals[2]);
                configFile.close();
                
            } catch (IOException e) {
                System.out.println("ERROR:  Exception:  " + e.toString());
            }
        }
        /*
        else
        {
            mBatteryLevel = b.batteryLevel;
            mReservoirAmount = i.totalInsulinAmount;
            mActiveProfileName = "";
        }
        */
    }
    public synchronized void writeCurrentValues(){
        
        try{
            SipLogger.Instance().info("Saving Simple Insulin Pump Configuration to SipConfig.txt");
            BufferedWriter configFile = new BufferedWriter(new FileWriter(msConfigFileName));
            //mReservoirAmount = newValue;
            //Getting corrected Battery by counting the seconds that have passed. 
            correctedBatteryLevel = roundTwoDecimals(((((BatteryLevel/100)*batteryLife)  - secondsUsed)/batteryLife)*100);
            //SipLogger.Instance().debug(Double.toString(correctedBatteryLevel));
            //SipLogger.Instance().debug(Double.toString(secondsUsed));
            configFile.write(Double.toString(correctedBatteryLevel)+eol);
            InitSip.totalInsulinAmount = roundFiveDecimals(InitSip.totalInsulinAmount);
            configFile.write(Double.toString(InitSip.totalInsulinAmount)+ eol);
            configFile.write(mActiveProfileName);
            //System.out.println(Double.toString(InsulinSensor.totalInsulinAmount) + " From Configuration File");
            configFile.close();
        
        }catch (IOException e) {
            System.out.println("ERROR:  Exception:  " + e.toString());
        }
        
        
    }
    
        public double roundTwoDecimals(double d){
        DecimalFormat twoDForm = new DecimalFormat("#.##");
        return Double.valueOf(twoDForm.format(d));
       }
        
        public double roundFiveDecimals(double d){
        DecimalFormat fiveDForm = new DecimalFormat("#.#####");
        return Double.valueOf(fiveDForm.format(d));
    }
    
   /* 
    public double getInitialBatteryLife()
    {
        return mBatteryLevel;
    }
    
    public double getInitialReservoirAmount()
    {
        return mReservoirAmount;
    }
    
    public String getActiveProfileName()
    {
        return mActiveProfileName;
    }
    */

}
